<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 企业信息相关Controller v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\enterprise;

use app\core\model\Enterprise;
use app\core\service\CallbackService;
use common\Encryption;
use common\Random;
use ding\model\CallbackTagEnum;
use think\Config;

/**
 * 企业信息相关操作
 *
 * @package app\core\controller
 */
class InfoController extends EnterpriseBaseController {
    /**
     * 企业信息配置
     *
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function config() {
        $this->_checkEnterpriseAdmin();
        $key = DEFAULT_DECRYPT_KEY;

        if (is_submit()) {
            $data = [];

            $data['contacter'] = input("post.contacter");
            $data['phone'] = input("post.phone");
            $data['logo'] = input("post.logo");
            $data['corpid'] = input("post.corpid");
            $data['corpsecret'] = input("post.corpsecret");
            $data['appid'] = input("post.appid");
            $data['appsecret'] = input("post.appsecret");

            $loginType = [];
            $loginType['ding'] = input("post.dingLogin") == 'on';
            $loginType['mobile'] = input("post.mobileLogin") == 'on';
            $loginType['username'] = input("post.usernameLogin") == 'on';
            $data['login_type'] = json_encode($loginType);

            $smsConfig = Enterprise::parseSmsconfig($this->enterprise);
            $smsImpl = fetch($smsConfig, 'impl');
            if (empty($smsImpl) || $smsImpl == 'ResourceSmsHelperImpl') {
                $smsConfig = [];
                $smsConfig['guestName'] = input("post.smsGuestName");
                $smsConfig['key'] = input("post.smsKey");
                $data['smsconfig'] = json_encode($smsConfig);
            }

            $homePage = [];
            $homePage['pc'] = input("post.homePagePc");
            $homePage['mobile'] = input("post.homePageMobile");
            $homePage['dingPc'] = input("post.homePageDingPc");
            $homePage['dingMobile'] = input("post.homePageDingMobile");
            $data['home_page'] = json_encode($homePage);

            if (!empty($data['corpid'])) {
                $data['corpid'] = Encryption::encrypt($data['corpid'], $key);
            }
            if (!empty($data['corpsecret'])) {
                $data['corpsecret'] = Encryption::encrypt($data['corpsecret'], $key);
            }
            if (!empty($data['appid'])) {
                $data['appid'] = Encryption::encrypt($data['appid'], $key);
            }
            if (!empty($data['corpid'])) {
                $data['appsecret'] = Encryption::encrypt($data['appsecret'], $key);
            }

            if (empty($data)) {
                $this->error("无更新内容");
            }

            Enterprise::update($data, $this->eid);
            $enterprise = Enterprise::get($this->eid);
            $this->user['enterprise'] = $enterprise;
            $this->_setUserSession($this->user);
            $this->success("保存成功");
        } else {
            $corpid = Encryption::decrypt($this->enterprise['corpid'], $key);
            $corpsecret = Encryption::decrypt($this->enterprise['corpsecret'], $key);
            $appid = Encryption::decrypt($this->enterprise['appid'], $key);
            $appsecret = Encryption::decrypt($this->enterprise['appsecret'], $key);
            $loginType = Enterprise::parseLoginType($this->enterprise);
            $smsConfig = Enterprise::parseSmsconfig($this->enterprise);
            $homePage = Enterprise::parseHomepage($this->enterprise);

            $smsGuestName = fetch($smsConfig, "guestName");
            $smsKey = fetch($smsConfig, "key");

            $this->assign("corpid", $corpid);
            $this->assign("corpsecret", $corpsecret);
            $this->assign("appid", $appid);
            $this->assign("appsecret", $appsecret);
            $this->assign("loginType", $loginType);
            $this->assign("smsConfig", [
                'guestName' => $smsGuestName,
                'key' => $smsKey
            ]);
            $this->assign("homePage", $homePage);

            return view();
        }
    }

    /**
     * 回调设置
     *
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function callback() {
        $this->_checkEnterpriseAdmin();
        $callback = Enterprise::getCallback($this->eid);
        $openBefore = fetch($callback, 'open', false);

        if (is_submit()) {
            $params = $this->request->post();
            $open = isset($params['open']) && $params['open'] == 'on';
            unset($params['open']);

            $tags = [];
            foreach ($params as $tag => $val) {
                if ($val == 'on') {
                    $tags[] = $tag;
                }
            }
            // 数据更新
            $callback['open'] = $open;
            $callback['tags'] = $tags;
            if (empty($callback['token'])) {
                $callback['token'] = Random::get();
            }
            if (empty($callback['aesKey'])) {
                $callback['aesKey'] = Random::get(43);
            }
            $ret = Enterprise::update([
                'callback' => json_encode($callback)
            ], $this->eid);

            if ($ret === false) {
                $this->error("设置失败");
            }

            $this->user['enterprise'] = Enterprise::get($this->eid);
            $this->_setUserSession($this->user);

            // 接口更新
            $callbackRet = CallbackService::update($this->eid, $openBefore, $open, $tags);
            if ($callbackRet !== true) {
                $this->error("接口设置失败:" . $callbackRet);
            }
            $this->success("设置成功");
        } else {
            $this->assign("callback", $callback);
            $this->assign("groups", CallbackTagEnum::group());
            return view();
        }
    }
}
